package com.liukai.bishe.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.user.User;
import com.liukai.bishe.presenter.userPresenter.IUpdateUserInfoPresenter;
import com.liukai.bishe.utils.Constants;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ChangeSexDialog{

    private Context mContext;
    private final AlertDialog.Builder mBuilder;

    public RelativeLayout btnMan;

    public RelativeLayout btnNoSex;

    public RelativeLayout btnWoman;
    private final View mView;

    private List<View> mCivViewList = new ArrayList<>();
    private List<View> mIvViewList = new ArrayList<>();
    private List<View> mTvViewList = new ArrayList<>();

    private CircleImageView mCivMan;
    private ImageView mIvMan;
    private TextView mTvMan;
    private CircleImageView mCivNoSex;
    private ImageView mIvNoSex;
    private TextView mTvNoSex;
    private CircleImageView mCivWoMan;
    private ImageView mIvWoMan;
    private TextView mTvWoMan;
    private TextView mBtnSure;

    private String sex = "保密";
    private final IUpdateUserInfoPresenter mIUpdateUserInfoPresenter;
    private AlertDialog mDialog;

    public ChangeSexDialog(Context context, int viewRes, IUpdateUserInfoPresenter iUpdateUserInfoPresenter) {

        this.mContext = context;
        //创建对话框的Builder
        mBuilder = new AlertDialog.Builder(mContext);
        // 创建对话框的View
        mView = View.inflate(mContext,viewRes,null);
        // 设置对话框的View
        mBuilder.setView(mView);
        // 获取到Activity的修改用户信息的Presenter
        this.mIUpdateUserInfoPresenter = iUpdateUserInfoPresenter;
        initView();
        initListener();

    }

    private void initView() {

        btnWoman = mView.findViewById(R.id.btnWoman);
        btnNoSex = mView.findViewById(R.id.btnNoSex);
        btnMan = mView.findViewById(R.id.btnMan);

        mCivMan = mView.findViewById(R.id.civMan);
        mIvMan = mView.findViewById(R.id.ivMan);
        mTvMan = mView.findViewById(R.id.tvMan);

        mCivNoSex = mView.findViewById(R.id.civNoSex);
        mIvNoSex = mView.findViewById(R.id.ivNoSex);
        mTvNoSex = mView.findViewById(R.id.tvNoSex);

        mCivWoMan = mView.findViewById(R.id.civWoMan);
        mIvWoMan = mView.findViewById(R.id.ivWoMan);
        mTvWoMan = mView.findViewById(R.id.tvWoMan);

        mBtnSure = mView.findViewById(R.id.btnSure);

    }

    private void initListener() {

//        // 男性按钮
        btnMan.setOnClickListener(view -> {
            clickMan();
            sex = "男";
        });

        // 性别保密按钮
        btnNoSex.setOnClickListener(view -> {
            clickNoSex();
            sex = "保密";

        });

        // 女性按钮
        btnWoman.setOnClickListener(view -> {
            clickWoman();
            sex = "女";
        });

        // 确定修改
        mBtnSure.setOnClickListener(view -> {

            User user = Constants.getUserInfo();
            user.setSex(sex);
            mIUpdateUserInfoPresenter.update(user);

            mDialog.dismiss();

        });

    }

    private void clickNoSex() {

        mCivMan.setImageResource(R.color.touming);
        mCivNoSex.setImageResource(R.color.colorNoSexGrey);
        mCivWoMan.setImageResource(R.color.touming);

        mIvMan.setImageResource(R.mipmap.man_normal);
        mIvNoSex.setImageResource(R.mipmap.no_sex_choose);
        mIvWoMan.setImageResource(R.mipmap.woman_normal);

        mTvMan.setTextColor(mContext.getResources().getColor(R.color.textColorMan));
        mTvNoSex.setTextColor(mContext.getResources().getColor(R.color.white));
        mTvWoMan.setTextColor(mContext.getResources().getColor(R.color.textColorWoMan));

    }

    private void clickMan() {

        mCivMan.setImageResource(R.color.textColorMan);
        mCivNoSex.setImageResource(R.color.touming);
        mCivWoMan.setImageResource(R.color.touming);

        mIvMan.setImageResource(R.mipmap.man_choose);
        mIvNoSex.setImageResource(R.mipmap.no_sex_normal);
        mIvWoMan.setImageResource(R.mipmap.woman_normal);

        mTvMan.setTextColor(mContext.getResources().getColor(R.color.white));
        mTvNoSex.setTextColor(mContext.getResources().getColor(R.color.colorNoSexGrey));
        mTvWoMan.setTextColor(mContext.getResources().getColor(R.color.textColorWoMan));

    }

    private void clickWoman() {

        mCivMan.setImageResource(R.color.touming);
        mCivNoSex.setImageResource(R.color.touming);
        mCivWoMan.setImageResource(R.color.textColorWoMan);

        mIvMan.setImageResource(R.mipmap.man_normal);
        mIvNoSex.setImageResource(R.mipmap.no_sex_normal);
        mIvWoMan.setImageResource(R.mipmap.woman_choose);

        mTvMan.setTextColor(mContext.getResources().getColor(R.color.textColorMan));
        mTvNoSex.setTextColor(mContext.getResources().getColor(R.color.colorNoSexGrey));
        mTvWoMan.setTextColor(mContext.getResources().getColor(R.color.white));

    }


    public void show(){
        mDialog = mBuilder.show();
    }


}
